const app = getApp()
import {
  getCartList,
  addCart

} from '../../api/user'
import {
  buyCart

} from '../../api/pay'


let currentPage = 1
let showCount = 10
let totalPage = 1
Page({
  data: {
    list: [],
    checked: false,
    activeId: [],
    nums: 0,
    money: 0,
    
  },
  onShow() {
    this.initCartList()
  },
  onReachBottom() {
    if (currentPage >= totalPage) {
      wx.showToast({
        title: '没有更多了',
        icon: 'none'
      })
      return
    }
    currentPage++
    this.fetchList()
  },
  onClickLeft() {
    // wx.showToast({
    //   title: '点击返回',
    //   icon: 'none'
    // });
    wx.navigateBack({
      delta: 1,
    })
  },
  goShop() {
    wx.switchTab({
      url: '/pages/shop/shop',
    })
  },
  addGoodNumber() {

  },
  initCartList() {
    currentPage = 1
    totalPage = 1
    this.setData({
      list: []
    })
    this.fetchCartList()
  },
  fetchCartList() {
    getCartList({
        showCount,
        currentPage,
      }).then(res => {
        console.log(res)
        const {
          list,
          totalPage: total
        } = res.data
        totalPage = total
        list.forEach((item) => {
          item.checked = false
          if(item.product){
            item.product.pic = item.product.pic.split(',')[0]
          }

        })
        this.setData({
          list: this.data.list.concat(list)
        })
      })
      .catch(err => {
        console.log(err)
      })
  },
  addGoodNumber(e) {
    const {id } = e.currentTarget.dataset
    const size = e.detail
    let {list} = this.data
    addCart({
      productId: String(id),
      size: String(size),
    }).then(res => {
      console.log(res)
      
      list.forEach( (item) => {
        if(item.productId == id){
          item.size = Number(size)
         
        }
      })
      if(size==0){
        list = list.filter( item => item.productId !=id)
      }
      this.setData(
        {
          list
        }
      )
      this.caclNum()
    })
    .catch( err => {

    })
  },
  onChange(e) {
    const target = Number(e.detail)
    const id = e.currentTarget.dataset.id
    const {
      list
    } = this.data
    list.forEach((item) => {
      if(item.shopcarId === id){
        item.checked = !item.checked
      }
    })
    this.setData({
      list,
    })

    console.log(e ,id)
    this.caclNum()
  },
  onChangeAll(e) {
    const target = Number(e.detail)
    const {
      list
    } = this.data
    const allList = list.map(item => {
      item.checked = target
      return item
    })

    
    this.setData({
      list: allList,
      checked: target
    })
    this.caclNum()
  },
  caclNum(){
    const {
      list
    } = this.data
    let money = 0;
    let nums = 0;
    list.forEach( (item ) => {
      if(item.checked){
        nums += item.size
        money += item.product.price * item.size
      }
    })

    this.setData({
      money,
      nums
    })
    
  },
  payCart(){
    let { list } = this.data
    
    list = list.filter( item => item.checked)
    if(list.length<=0){
      return;
    }
    const jsonKeyList =  list.map((item) =>  item.productId)
    const jsonKeySize =  list.map((item) =>  item.size)
    const jsonKey = jsonKeyList.join(',')
    const jsonVal = {}
    console.log(list , jsonKeyList ,jsonKeySize)
    for(let i = 0; i< jsonKeyList.length ;i++){
      let item = jsonKeyList[i]
      let size = jsonKeySize[i]
      jsonVal[item] =  size
    }
    buyCart({
      jsonKey: String(jsonKey),
      jsonVal: JSON.stringify(jsonVal)
    }).then(res => {
      console.log(res)
      this.initCartList()
      wx.showToast({
        title: '结算成功',
      })
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/order/order?status=0',
        })
      }, 2000);
    })
    .catch(err => {
      console.log(err)
    })
    console.log( jsonKey, jsonVal)
  }
})